package com.chinaedu.oc.util.common;

import java.lang.reflect.Field;

import com.chinaedu.oc.util.annotation.Instruction;

/**
 * 属性判空工具类
 * @Description: TODO
 * @author A18ccms a18ccms_gmail_com 
 * @date 2017年5月26日 下午12:07:16 
 *
 */
public class CheckNull {
	private String info = "为空";
	public void ckeckNull(Object t, String[] fields) throws CheckParamException {
		this.checkNull(t, fields, info);
	}
	public void checkNull(Object t, String... str) throws CheckParamException {
		this.checkNull(t, str, info);
	}
	/**
	 * 属性判空
	 * @Description: TODO
	 * @param @param t 对象
	 * @param @param fields 属性列表
	 * @param @param infos 错误信息
	 * @param @throws CheckParamException    
	 * @return void    返回类型 
	 * @throws
	 */
	public void checkNull(Object t, String[] fields, String infos) throws CheckParamException{
				
		if(fields==null) {
			throw new RuntimeException("字段数组数据错误...");
		}
		
		if(t == null) {
			throw new CheckParamException("数据为空");
		}
		
		Class clazz = t.getClass();
		
		
		String msg = "";
		for(int i=0; i<fields.length; i++) {
			
			String s = fields[i];
			
			
			Field f = null;
			try {
				f = clazz.getDeclaredField(s);
				f.setAccessible(true);
				
				
				String info = f.getName();
				Instruction an = f.getAnnotation(Instruction.class);
				if(an != null) {
					String in = an.value();
					if(!StringUtil.isBlack(in)) {
						info = in;
					}
				}
				
				Object o = f.get(t);
				if(String.class.equals(f.getType())){
					String d = (String) o;
					if(StringUtil.isBlack(d)) {
						//throw new CheckParamException(info+infos);
						msg += info+",";
					}
				}else {
					if(o == null) {
						//throw new CheckParamException(info+infos);
						msg += info+",";
					}
				}
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		if(!"".equals(msg)) {
			throw new CheckParamException(msg.substring(0, msg.length()-1)+infos);
		}
	}
}
